using System;
using System.Collections.Generic;
using System.Text;
using Instedd.Labs.Geo.Library.Kml;
using System.Globalization;

namespace Instedd.Labs.Geo.Library.Sms
{
    internal static class BubbleDescriptionGenerator
    {
        internal static string GetDescription(string name, LatLong location, IEnumerable<SmsExtension> messages, IBubbleDescriptionSettings settings , out SmsExtension lastUserMessage)
        {
            lastUserMessage = null;

            StringBuilder description = new StringBuilder();
            foreach (SmsExtension message in messages)
            {
                description.Append(string.Format("<b>{0}:</b> {1}<br/>", message.SenderName, message.Message));
                if (!message.IsReplay)
                {
                    lastUserMessage = message;
                }
            }
            if (settings.IncludeReplyLink)
            {
                description.Append(GenerateReplyLink(lastUserMessage, settings.ReplyLinkTemplate));
            }
            
            description.Insert(0, string.Format("<i>Last message at {0}</i><br/>",  
                lastUserMessage.Sms.Time.ToString("HH:mm (MM-dd-yyyy)", CultureInfo.CurrentUICulture)));
            
            if (settings.IncludeSenderNumber && name != lastUserMessage.Sms.Number)
            {
                description.Insert(0, string.Format("Sender Number: {0}<br/>", lastUserMessage.Sms.Number));                
            }

            return description.ToString();
        }

        internal static string GenerateReplyLink(SmsExtension message, string linkTemplate)
        {
            if (!string.IsNullOrEmpty(linkTemplate))
            {
                string link = string.Format(" <a href=\"{0}\">Reply</a>", linkTemplate);
                return string.Format(link, message.SenderName, message.Message, message.Sms.Number, message.GetMessageIdAsString());
            }
            else
            {
                return string.Empty;
            }
        }
    }
}
